#pragma once
#include "esp_log.h"
#include "esp_idf_version.h"
#include "esp_heap_caps.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <stdio.h>
#include "esp_log.h"

#ifndef CONFIG_LOG_MAXIMUM_LEVEL
#define CONFIG_LOG_MAXIMUM_LEVEL ESP_LOG_DEBUG
#endif

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
#define CONFIG_USE_OLD_I2C_DRIVER 0
#else
#define CONFIG_USE_OLD_I2C_DRIVER 1
#endif

#if CONFIG_USE_OLD_I2C_DRIVER == 0
#include "driver/i2c_master.h"
esp_err_t i2c_master_init();
extern i2c_master_bus_handle_t i2c_handle;
#else
#include "driver/i2c.h"
esp_err_t bsp_i2c_init(void);
#endif

#define SCL_IO_PIN 2
#define SDA_IO_PIN 1
#define MASTER_FREQUENCY 200000
#define I2C_PORT 0
#define LENGTH 48
#define I2C_TIMEOUT_MS 1000
#define PCA9557_ADDR 0X19
#define FT6336_ADDR 0X38

#define PCA9557_INPUT_PORT 0x00
#define PCA9557_OUTPUT_PORT 0x01
#define PCA9557_POLARITY_INVERSION_PORT 0x02
#define PCA9557_CONFIGURATION_PORT 0x03

void i2c_init(void);
void pca9557_init(void);

esp_err_t ft6336_register_read(uint8_t reg_addr, uint8_t *data, size_t len);
esp_err_t ft6336_register_write_byte(uint8_t reg_addr, uint8_t data);
void lcd_cs(uint8_t level);
void pa_en(uint8_t level);
void dvp_pwdn(uint8_t level);

#define SET_BITS(_m, _s, _v) ((_v) ? (_m) | ((_s)) : (_m) & ~((_s)))